package com.agriculture.data_demo.service;

import com.agriculture.data_demo.dto.PageResult;
import com.agriculture.data_demo.entity.sys.BroilerBatchEntity;
import com.agriculture.data_demo.entity.sys.BroilerDailyEntity;
import com.agriculture.data_demo.entity.sys.BroilerEnvEntity;
import com.agriculture.data_demo.repository.BroilerBatchRepository;
import com.agriculture.data_demo.repository.BroilerDailyRepository;
import com.agriculture.data_demo.repository.BroilerEnvRepository;
import com.agriculture.data_demo.vo.sys.BroilerBatchVo;
import com.agriculture.data_demo.vo.sys.BroilerDailyVo;
import com.agriculture.data_demo.vo.sys.BroilerEnvVo;
import com.agriculture.data_demo.vo.sys.BroilerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author morningbear
 * @date 20.10.7
 * @update 20.10.19
 */
@Slf4j
@Service
public class BroilerService {

    @Resource
    private BroilerBatchRepository broilerBatchRepository;
    @Resource
    private BroilerDailyRepository broilerDailyRepository;
    @Resource
    private BroilerEnvRepository broilerEnvRepository;


    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 肉鸡环境数据
     */
    public PageResult<BroilerEnvVo> getAllBroilerEnv(Integer pageNumber, Integer pageSize){
        Page<BroilerEnvEntity> page = broilerEnvRepository.findAll(PageRequest.of(pageNumber, pageSize));
        return getBroilerEnvVoPageResult(pageNumber, pageSize, page);
    }

    /**
     *
     * @param startDate 起始日期
     * @param endDate 截至日期
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 一段时间内的肉鸡生产环境数据
     */
    public PageResult<BroilerEnvVo> getBroilerEnvByDate(Date startDate, Date endDate, Integer pageNumber, Integer pageSize){
        Page<BroilerEnvEntity> page = broilerEnvRepository.findBroilerEnvEntitiesByDate(startDate, endDate, PageRequest.of(pageNumber, pageSize));

        return getBroilerEnvVoPageResult(pageNumber, pageSize, page);
    }

    private PageResult<BroilerEnvVo> getBroilerEnvVoPageResult(Integer pageNumber, Integer pageSize, Page<BroilerEnvEntity> page) {
        List<BroilerEnvVo> broilerEnvVoList = new ArrayList<>();
        for (BroilerEnvEntity broilerEnvEntity : page.getContent()){
            BroilerEnvVo broilerEnvVo = new BroilerEnvVo();
            BeanUtils.copyProperties(broilerEnvEntity, broilerEnvVo);
            broilerEnvVoList.add(broilerEnvVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), broilerEnvVoList);
    }

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 肉鸡生产周期数据
     */
    public PageResult<BroilerDailyVo> getAllBroilerDaily(Integer pageNumber, Integer pageSize){
        Page<BroilerDailyEntity> page = broilerDailyRepository.findAll(PageRequest.of(pageNumber, pageSize));

        return getBroilerDailyVoPageResult(pageNumber, pageSize, page);
    }

    /**
     *
     * @param startDate 起始日期
     * @param endDate 截至日期
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 一段时间内的肉鸡生产周期数据
     */
    public PageResult<BroilerDailyVo> getBroilerDailyByDate(Date startDate, Date endDate, Integer pageNumber, Integer pageSize){
        Page<BroilerDailyEntity> page = broilerDailyRepository.findBroilerDailyEntitiesByDate(startDate, endDate, PageRequest.of(pageNumber, pageSize));

        return getBroilerDailyVoPageResult(pageNumber, pageSize, page);
    }

    private PageResult<BroilerDailyVo> getBroilerDailyVoPageResult(Integer pageNumber, Integer pageSize, Page<BroilerDailyEntity> page) {
        List<BroilerDailyVo> broilerDailyVoList = new ArrayList<>();
        for (BroilerDailyEntity broilerDailyEntity : page.getContent()){
            BroilerDailyVo broilerDailyVo = new BroilerDailyVo();
            BeanUtils.copyProperties(broilerDailyEntity, broilerDailyVo);
            broilerDailyVoList.add(broilerDailyVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), broilerDailyVoList);
    }

    /**
     *
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 肉鸡生产批次数据
     */
    public PageResult<BroilerBatchVo> getAllBroilerBatch(Integer pageNumber, Integer pageSize){
        Page<BroilerBatchEntity> page = broilerBatchRepository.findAll(PageRequest.of(pageNumber, pageSize));

        return getBroilerBatchVoPageResult(pageNumber, pageSize, page);
    }

    /**
     *
     * @param startDate 起始日期
     * @param endDate 截至日期
     * @param pageNumber 请求页数
     * @param pageSize 页大小
     * @return 某个时间段的肉鸡生产批次数据
     */
    public PageResult<BroilerBatchVo> getBroilerBatchByDate(Date startDate, Date endDate, Integer pageNumber, Integer pageSize){
        Page<BroilerBatchEntity> page = broilerBatchRepository.findBroilerBatchEntitiesByDate(startDate, endDate, PageRequest.of(pageNumber, pageSize));

        return getBroilerBatchVoPageResult(pageNumber, pageSize, page);
    }

    private PageResult<BroilerBatchVo> getBroilerBatchVoPageResult(Integer pageNumber, Integer pageSize, Page<BroilerBatchEntity> page) {
        List<BroilerBatchVo> broilerBatchVoList = new ArrayList<>();
        for (BroilerBatchEntity broilerBatchEntity : page.getContent()){
            BroilerBatchVo broilerBatchVo = new BroilerBatchVo();
            BeanUtils.copyProperties(broilerBatchEntity, broilerBatchVo);
            broilerBatchVoList.add(broilerBatchVo);
        }
        return new PageResult<>(pageNumber, pageSize, page.getTotalPages(), broilerBatchVoList);
    }

}
